﻿using NetOffice.VBIDEApi;
using NetOffice.OfficeApi;
using NetOffice.OfficeApi.Enums;

namespace 枸杞VBE插件.ComHost
{
    internal static class Extension
    {
        internal static CommandBarButton AddButton(this CommandBarControls controls, string name, int FaceId, CommandBarButton_ClickEventHandler ClickEvent, string? key = null)
        {
            CommandBarButton Button = (CommandBarButton)controls.Add(MsoControlType.msoControlButton);
            Button.Caption = name;
            Button.FaceId = FaceId;
            Button.Style = MsoButtonStyle.msoButtonIconAndCaption;
            Button.Tag = "控件" + ThisAddin.Instance.controlIndex++;
            Button.ClickEvent += ClickEvent;
            ThisAddin.Instance.ControlDictionary[key ?? name] = Button;
            //Log.Info("添加按钮:{0}", 字典key ?? 名称);
            return Button;
        }

        internal static CommandBarPopup AddMenu(this CommandBarControls Controls, string name, string? key = null)
        {
            CommandBarPopup Menu = (CommandBarPopup)Controls.Add(MsoControlType.msoControlPopup);
            Menu.Caption = name;
            if (key != null) ThisAddin.Instance.ControlDictionary[key] = Menu;
            //Log.Info("添加菜单:{0}", 字典key ?? 名称);
            return Menu;
        }

        internal static void Deleteblanklines(this CodeModule Module)
        {
            for (int i = Module.CountOfLines; i > 0; i--)
            {
                if (Module.Lines(i, 1).Trim().Length == 0) Module.DeleteLines(i, 1);
            }
        }
    }
}